package btd.game;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class DBTools {
	public static final String KEY_ROWID = "_id";
	public static final String KEY_NAME = "name";
	public static final String KEY_PWD = "pwd";
	public static final String KEY_LANGUAGE = "language";
	public static final String KEY_COUNTRY = "country";
	public static final String KEY_ISLOGINED = "islogined";
	public static final String KEY_YINXIAO = "yinxiao";
	public static final String KEY_DUMIAO = "dumiao";
	public static final String KEY_JINRUDIAN = "jinrudian";
	private static final String DATABASE_NAME = "weiqi";
	private static final String DATABASE_TABLE = "userinfo";
	private static final String DATABASE_CREATE_TABLE = "create table "
			+ DATABASE_TABLE + " ("
			+ KEY_ROWID + " integer primary key autoincrement, "
			+ KEY_NAME + " text not null, "
			+ KEY_PWD + " text not null, "
			+ KEY_LANGUAGE + " text not null, "
			+ KEY_COUNTRY + " text not null, "
			+ KEY_ISLOGINED + " text not null, "
			+ KEY_YINXIAO + " text not null, "
			+ KEY_DUMIAO + " text not null, "
			+ KEY_JINRUDIAN + " text not null);";
	private static final String DATABASE_TABLE2 = "optioninfo";
	private static final String DATABASE_CREATE_TABLE2 = "create table "
			+ DATABASE_TABLE2 + " (" + KEY_LANGUAGE + " text not null" + ");";
	private Context context = null;
	private SQLiteDatabase db = null;
	private boolean flag = true;

	public DBTools(Context context) {
		this.context = context;
	}

	private void openOrCreateDB() {
		boolean has = false;
		try {
			String[] list = this.context.databaseList();
			for (int i = 0; i < list.length; i++) {
				if (list[i].equals(DATABASE_NAME)) {
					has = true;
					break;
				}
			}
			if(db != null && db.isOpen()) {
				db.close();
			}
			db = this.context.openOrCreateDatabase(DATABASE_NAME,
					Context.MODE_PRIVATE, null);
			// Log.i("ocDB", "ok");
			// Log.i("has", String.valueOf(has));
		} catch (SQLException e) {
			// Log.i("ocDB", e.toString());
			return;
		}
		if (!has) {
			try {
				// System.out.println("vvvvvvvvvvv");
				db.execSQL(DATABASE_CREATE_TABLE);
				db.execSQL(DATABASE_CREATE_TABLE2);

				// Log.i("db.execSQL", "ok");
			} catch (SQLException e) {
				// Log.i("db.execSQL", e.toString());
			}
		}
	}

	private void createConstTextsTable() {
		boolean has = false;
		try {
			String[] list = this.context.databaseList();
			for (int i = 0; i < list.length; i++) {
				if (list[i].equals("consttextinfo")) {
					has = true;
					break;
				}
			}
			db = this.context.openOrCreateDatabase(DATABASE_NAME,
					Context.MODE_PRIVATE, null);
			// Log.i("ocDB", "ok");
			Log.i("has", String.valueOf(has));
		} catch (SQLException e) {
			// Log.i("ocDB", e.toString());
			return;
		}
		if (!has) {
			try {
				StringBuffer createTable = new StringBuffer();
				createTable.append("create table consttextinfo ");
				createTable.append("(id integer primary key autoincrement, ");
				createTable.append("textinfo text not null);");
				// System.out.println("vvvvvvvvvvv");
				db.execSQL(createTable.toString());

				// Log.i("db.execSQL", "ok");
			} catch (SQLException e) {
				// Log.i("db.execSQL", e.toString());
			}
		}
	}
	
	public String getConstTexts() {
		createConstTextsTable();
		String info = "";
		try {
			Cursor cur = db.query("consttextinfo", null, null, null, null, null,
					null);
			Log.i("cur.Count", String.valueOf(cur.getCount()));
			if(cur.getCount() <= 0) {
				context.deleteDatabase("weiqi");
				updateInfo("scliang", "123456", ConstData.LANG_ZH, "china", "F", "1", "o", "o");
				updateConstTexts("nihao_nihao_nihao_nihao_nihao");
			}
			if (cur.moveToFirst()) {
				// Log.i("cur.ColumnCount",
				// String.valueOf(cur.getColumnCount()));
				info = cur.getString(cur.getColumnIndex("textinfo"));
				// Log.i("db.query", "ok");
			}
		} catch (SQLException e) {
			Log.i("db.query", e.toString());
		}
		closeDB();
		return info;
	}

	public void updateConstTexts(String info) {
		createConstTextsTable();
		try {
			Cursor cur = db.query("consttextinfo", null, null, null, null,
					null, null);
			// System.out.println("----cur---");
			String sql = "";
			if (cur.getCount() > 0) {
				// System.out.println("ok");
				if (cur.moveToFirst()) {
					sql = "update consttextinfo set textinfo = '"  + info + "';";
				}
			} else {
				sql = "insert into consttextinfo (textinfo) values ('" + info + "');";
				// System.out.println(sql);
			}
			if (!("").equals(sql)) {
				db.execSQL(sql);
			}
			// Log.i("db.uptate", "ok");
		} catch (SQLException e) {
			// System.out.println(e.getMessage());
			// Log.i("db.uptate", e.toString());
		}
		closeDB();
	}

	private void closeDB() {
		try {
			if (db != null) {
				db.close();
				db = null;
			}
			// Log.i("coDB", "ok");
		} catch (SQLException e) {
			// Log.i("coDB", e.toString());
		}
	}

	// sqlite_array_query
	public String getInfo() {
		openOrCreateDB();
		String info = "";
		try {
			Cursor cur = db.query(DATABASE_TABLE, null, null, null, null, null,
					null);
			Log.i("cur.Count", String.valueOf(cur.getCount()));
			if(cur.getCount() <= 0) {
				context.deleteDatabase("weiqi");
				updateInfo("scliang", "123456", ConstData.LANG_ZH, "CHN", "F", "1", "o", "o");
				updateConstTexts("nihao_nihao_nihao_nihao_nihao");
			}
			if (cur.moveToFirst()) {
				// Log.i("cur.ColumnCount",
				// String.valueOf(cur.getColumnCount()));
				info = cur.getString(cur.getColumnIndex(KEY_NAME)) + "_"
						+ cur.getString(cur.getColumnIndex(KEY_PWD)) + "_"
						+ cur.getString(cur.getColumnIndex(KEY_LANGUAGE)) + "_"
						+ cur.getString(cur.getColumnIndex(KEY_COUNTRY)) + "_"
						+ cur.getString(cur.getColumnIndex(KEY_ISLOGINED)) + "_"
						+ cur.getString(cur.getColumnIndex(KEY_YINXIAO)) + "_"
						+ cur.getString(cur.getColumnIndex(KEY_DUMIAO)) + "_"
						+ cur.getString(cur.getColumnIndex(KEY_JINRUDIAN));
				// Log.i("db.query", "ok");
			}
		} catch (SQLException e) {
			Log.i("db.query", e.toString());
		}
		closeDB();
		return info;
	}

	public void updateInfo(String name, String pwd, String language,
			String country, String islogined, String yinxiao, String dumiao, String jinrudian) {
		openOrCreateDB();
		try {
			Cursor cur = db.query(DATABASE_TABLE, null, null, null, null, null,
					null);
			String sql = "";
			if (cur.getCount() > 0) {
				if (cur.moveToFirst()) {
					sql = "update " + DATABASE_TABLE + " set "
					+ KEY_NAME + "='" + name + "', "
					+ KEY_PWD + "='" + pwd + "', "
					+ KEY_LANGUAGE + "='" + language + "', "
					+ KEY_ISLOGINED + "='" + islogined + "', "
					+ KEY_YINXIAO + "='" + yinxiao + "', "
					+ KEY_DUMIAO + "='" + dumiao + "', "
					+ KEY_JINRUDIAN + "='" + jinrudian
					+ "' where " + KEY_ROWID + "=" + cur.getInt(0) + ";";
				}
			} else {
				sql = "insert into " + DATABASE_TABLE + " ("
				+ KEY_NAME + ", "
				+ KEY_PWD + ", "
				+ KEY_LANGUAGE + ", "
				+ KEY_COUNTRY + ", "
				+ KEY_ISLOGINED + ", "
				+ KEY_YINXIAO + ", "
				+ KEY_DUMIAO + ", "
				+ KEY_JINRUDIAN + ") values ('"
				+ name + "', '"
				+ pwd + "', '"
				+ language + "', '"
				+ country + "', '"
				+ islogined + "', '"
				+ yinxiao + "', '"
				+ dumiao + "', '"
				+ jinrudian + "');";
			}
			if (!("").equals(sql)) {
				db.execSQL(sql);
			}
			Log.i("db.uptate", "ok");
		} catch (SQLException e) {
			Log.i("db.uptate", e.toString());
		}
		ConstData.YINXIAO = yinxiao;
		ConstData.DUMIAO = dumiao;
		ConstData.JINRUDIAN = jinrudian;
		closeDB();
	}

	public String getInfo2() {
		String[] infos = getInfo().split("_");
		if(infos.length == 8) {
			return infos[2];
		}
		return "";
	}

	public void updateInfo2(String language) {
		String[] infos = getInfo().split("_");
		if(infos.length == 8) {
			updateInfo(infos[0], infos[1], language, infos[3], 
					infos[4], infos[5], infos[6], infos[7]);
		}
		closeDB();
	}
}
